using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerDashState : PlayerAbilityState
{
    public PlayerDashState(Player player, PlayerMoveData playerData, StateMachine stateMachine, string animBoolName) : base(player, playerData, stateMachine, animBoolName)
    {
    }

    private Vector2Int dashDir;

    private Vector3 endPos;

    public void SetDashDirection(Vector2Int dir) { dashDir = dir; }

    public override void Enter()
    {
        base.Enter();
        canUseAbility = false;
        player.SetVelocityZero();
        player.Rb2D.gravityScale = 0;
        player.CheckIfNeedFlip(dashDir.x);

        ShadowSpwaner.Instance.ShowShadow(player.gameObject, 5, playerData.dashTime);
    }

    public override void LogicUpdate()
    {
        base.LogicUpdate();

        if (!isAbilityDone) 
        {
            player.SetVelocityX(dashDir.x * playerData.dashVelocity.x);
            player.SetVelocityY(dashDir.y * playerData.dashVelocity.y);
        }
       
        if (!isAbilityDone && (Time.time - stateEnterTime >= playerData.dashTime || (dashDir.y == 1 && isTouchingCeiling) || isTouchingWall || (dashDir.y == -1 && isGrounded)))
        {
            endPos = player.transform.position;
            isAbilityDone = true;
        }
    }

    public override void Exit()
    {
        base.Exit();
        player.SetVelocityZero();
        player.transform.position = endPos;
        player.Rb2D.gravityScale = playerData.gravityScale;
    }

    public bool CanDash() 
    {
        return canUseAbility || Time.time - lastUseTime > playerData.dashCoolTime;
    }
}
